package cd.cdyb.sms.controller;

import cd.cdyb.sms.domain.Signature;
import cd.cdyb.sms.dto.PageResult;
import cd.cdyb.sms.dto.SmsDto;
import cd.cdyb.sms.dto.expand.ExSignatureDto;
import cd.cdyb.sms.dto.query.SignatureQueryDto;
import cd.cdyb.sms.exception.BusinessException;
import cd.cdyb.sms.service.SignatureService;
import cd.cdyb.sms.service.SmsService;
import cd.cdyb.sms.utils.UserUtils;
import cn.hutool.core.lang.UUID;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/signature")
public class SignatureController {

    @Resource
    private SignatureService signatureService;

    @Resource
    private SmsService smsService;

    /**
     * 新增
     */
    @PostMapping("/insertSignature")
    public void insertSignature(@RequestBody @Valid ExSignatureDto exSignatureDto) throws BusinessException {
        exSignatureDto.setCompanyId(UserUtils.getCompanyId());
        signatureService.insertSignature(exSignatureDto);
    }

    /**
     * 客户端 列表
     */
    @PostMapping("/findSignatureList")
    public List<ExSignatureDto> findSignatureList(@RequestBody SignatureQueryDto exSignatureDto) throws BusinessException {
        exSignatureDto.setCompanyId(UserUtils.getCompanyId());
        return signatureService.findSignatureList(exSignatureDto);
    }

    /**
     * 客户端分页
     */
    @PostMapping("/findSignaturePageWithClient")
    public PageResult<ExSignatureDto> findSignaturePageWithClient(@RequestBody SignatureQueryDto exSignatureDto) throws BusinessException {
        exSignatureDto.setCompanyId(UserUtils.getCompanyId());
        return signatureService.findSignaturePage(exSignatureDto);
    }

    /**
     * 查询详情
     */
    @PostMapping("/getSignatureById")
    public Signature getSignatureById(@RequestBody ExSignatureDto exSignatureDto) throws BusinessException {
        return signatureService.getSignatureById(exSignatureDto.getSignatureId());
    }

    /**
     * 管理端分页
     */
    @PostMapping("/findSignaturePageWithAdmin")
    public PageResult<ExSignatureDto> findSignaturePageWithAdmin(@RequestBody SignatureQueryDto exSignatureDto) {
        return signatureService.findSignaturePage(exSignatureDto);
    }

    /**
     * 首页签名统计-客户端
     **/
    @PostMapping("/indexSignatureStatisticsWithClient")
    public Map<String, Long> indexSignatureStatisticsWithClient() throws BusinessException {
        return signatureService.indexSignatureStatisticsWithClient(UserUtils.getCompanyId());
    }

    @PostMapping("/send")
    public void indexSignatureStatisticsWithClient(@RequestBody SmsDto smsDto) throws BusinessException {
        smsService.sendMessage(smsDto.getPhone(), smsDto.getTemplateId(), smsDto.getContent(), smsDto.getType(), UUID.randomUUID().toString().replace("-", "").substring(0, 20));
    }
}
